package com.gxa.sso.repository;

import com.gxa.sso.pojo.Employee;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Created by zxd on 2020/12/15
 **/

public interface EmployeeRepository extends JpaRepository<Employee,Long> {

    //命名规范
    public Employee findEmployeeByName(String name);

    //根据主键查询
    public Employee findEmployeeById(Long id);

    //where name like ?% and age<?
    public List<Employee> findEmployeeByNameStartingWithAndAgeLessThan(String name,Integer age);

    //where name like ?% and age>=?
    public List<Employee> findEmployeeByNameStartingWithAndAgeGreaterThanEqual(String name,Integer age);

    //where name in(?,?,?) and age<=?
    public List<Employee> findEmployeeByNameInAndAgeLessThanEqual(List<String> names,Integer age);

    //where name like '%?%' limit ?,?
    public Page<Employee> findEmployeeByNameContaining(String name, Pageable pageable);


    //写sql
    //hql
    @Query("select o from Employee o where age=(select max(age) from Employee)")
    public Employee getEmployeeMaxAge();

    @Query("select o from Employee o where o.age=?1 and o.name=?2")
    public List<Employee> queryParam1(Integer age,String name);

    @Query("select o from Employee o where o.age=:a and o.name=:n")
    public List<Employee> queryParam2(@Param("a") Integer age,@Param("n") String name);

    //原生的sql
    @Query(nativeQuery = true,value = "select count(*) from gxa_employee")
    public long getCount();

}
